import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getLocalToken, setLocalToken} from '@/utils/auth'
import type { Profile } from '@/types/user'
import { getProfileAPI } from '@/apis/user'

export const useUserStore = defineStore('hm-user', () => {
  // 1. token的处理
  const token = ref(getLocalToken() || '')
  const setToken = (t: string)=>{
    token.value = t
    // 往cookie中存储一份
    setLocalToken(t)
  }

  // 2. 用户权限的处理
  const profile = ref<Profile>({} as Profile)

  const getProfile = async ()=>{
    const res = await getProfileAPI()
    profile.value = res.data
    return res.data
  }

  return {token, setToken, profile, getProfile}
})
